﻿using System;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BikeBouncer.BusinessLayer;
using BikeBouncer.Site.Enums;

namespace BikeBouncer.Site
{
	public class BaseBoxPage : BasePage
	{
		#region Properties

		private new String H1 { get; set; }

		protected new String ErrorTitle
		{
			get
			{
				return ((BoxLayout_master)Master).ErrorTitle;
			}
			set
			{
				((BoxLayout_master)Master).ErrorTitle = value;
			}
		}

		protected new Int32 ErrorCount
		{
			get
			{
				return ((BoxLayout_master)Master).Errors.Controls.Count;
			}
		}

		#endregion

		#region Custom viewstate

		protected override PageStatePersister PageStatePersister
		{
			get
			{
				return new SessionPageStatePersister(this);
			}
		}

		#endregion

		#region Main methods

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			Object title = GetLocalResourceObject("PageTitle");
			Object description = GetLocalResourceObject("PageDescription");

			if (title != null)
			{
				Title = title.ToString();
			}

			if (description != null)
			{
				Description = description.ToString();
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			String defaultTitle = GetGlobalResourceObject("Messages", "DefaultPageTitle").ToString();

			if (String.IsNullOrEmpty(Title))
			{
				Title = defaultTitle;
			}
			else
			{
				Title = Title + " - " + defaultTitle;
			}

			((HtmlMeta)Header.FindControl("MetaDescription")).Content = Description;
		}

		#endregion
		
		#region Errors

		protected new void AppendError(Exception ex)
		{
			AppendError(GetGlobalString("Error") + ": " + ex.Message);
			ErrorManager.Log(ex);
		}

		protected new void AppendError(Exception ex, String prefix)
		{
			AppendError(prefix + ": " + ex.Message);
			ErrorManager.Log(ex);
		}

		protected new void AppendError(String message)
		{
			HtmlGenericControl li = new HtmlGenericControl("li");
			li.InnerHtml = message;

			((BoxLayout_master)Master).Errors.Controls.Add(li);
		}

		#endregion
	}
}